//
//  AllTaskViewController.m
//  firstpoint
//
//  Created by 泽中 on 2017/6/28.
//  Copyright © 2017年 xzcysoft. All rights reserved.
//
//#import "MJRefresh.h"
#import "AllTaskViewController.h"
#import "LeeAllRenWuTableViewCell.h"
#import "RenWuXiangQingViewController.h"
#import "LoadMoreViewController.h"
@interface AllTaskViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView*tabView;
    int page;
}
@property(nonatomic,strong)NSMutableArray *bigMUArr;
@property(nonatomic,strong)NSArray *cainiarray;
@end

@implementation AllTaskViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    page=0;
    
    [self setTab];
    [self httpurlRquestcainixihuan];//猜你喜欢
    [self httpRenwuUrl];//全部任务
    
    self.view.backgroundColor=[UIColor whiteColor];
}
-(void)setTab
{
    tabView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, width_screen, height_screen-64-49-60) style:UITableViewStyleGrouped];
//    tabView.separatorStyle=0;
    tabView.delegate=self;
//    tabView.mj_header
    tabView.separatorColor = COLOR(229, 230, 231, 1);//分割线颜色
    tabView.dataSource=self;
    [tabView registerNib:[UINib nibWithNibName:@"LeeAllRenWuTableViewCell" bundle:nil] forCellReuseIdentifier:@"OenCell"];
    [self.view addSubview:tabView];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section==0) {
        UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, width_screen, 100)];
        UIButton*button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, width_screen, 40)];
        button.backgroundColor=LRRGBColor(250, 250, 250);
        [button setTitle:@"加载更多" forState:0];
        [button addTarget:self action:@selector(jiazaiGengduoBtnClick) forControlEvents:UIControlEventTouchUpInside];
        button.titleLabel.font=[UIFont systemFontOfSize:14];
        [button setTitleColor:LRRGBColor(22, 141, 210) forState:0];
        [view addSubview:button];
        UIView*huiseView=[[UIView alloc]initWithFrame:CGRectMake(0, 40, width_screen, 10)];
        huiseView.backgroundColor=LRRGBColor(235, 240, 243);
        [view addSubview:huiseView];
        
        UIView*xiamianView=[[UIView alloc]initWithFrame:CGRectMake(0, 50, width_screen, 40)];
        xiamianView.backgroundColor=[UIColor whiteColor];
        UILabel*titleLab=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, 150, 40)];
        titleLab.text=@"猜您喜欢";
        titleLab.textColor=LRRGBColor(22, 141, 210);
        [xiamianView addSubview:titleLab];
        [view addSubview:xiamianView];
        
        UIButton*huanyipiBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        huanyipiBtn.frame=CGRectMake(width_screen-80, 0, 80, 40);
        [huanyipiBtn setTitle:@"换一批" forState:0];
        [huanyipiBtn addTarget:self action:@selector(httpurlRquestcainixihuan) forControlEvents:UIControlEventTouchUpInside];
        [huanyipiBtn setTitleColor:LRRGBColor(22, 141, 210) forState:0];
        huanyipiBtn.titleLabel.font=[UIFont systemFontOfSize:14];
        [xiamianView addSubview:huanyipiBtn];
        return view;
    }else{
        UIButton*button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, width_screen, 40)];
        button.backgroundColor=LRRGBColor(250, 250, 250);
        [button setTitle:@"换一批" forState:0];
        button.titleLabel.font=[UIFont systemFontOfSize:14];
        [button addTarget:self action:@selector(httpurlRquestcainixihuan) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor:LRRGBColor(22, 141, 210) forState:0];
        
        return button;
    }
    return nil;
}
//加载更多任务
-(void)jiazaiGengduoBtnClick
{
    LoadMoreViewController*vc=[LoadMoreViewController new];
    vc.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:vc animated:YES];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
//        return 4;
        return _bigMUArr.count;
    }
    return _cainiarray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section==0) {
        return 95;

    }
    return 40;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LeeAllRenWuTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"OenCell"];
    if (indexPath.section==0) {
        if (_bigMUArr.count>0) {
            cell.titleLabel.text = [NSString stringWithFormat:@"%@",_bigMUArr[indexPath.row][@"title"]];//b标题
            //参与任务
            cell.canyuLbale.text = [NSString stringWithFormat:@"参与人数 %@/%@",_bigMUArr[indexPath.row][@"finCounts"],_bigMUArr[indexPath.row][@"peopleCount"]];
            int aa = [_bigMUArr[indexPath.row][@"finCounts"]intValue];
            int as = [_bigMUArr[indexPath.row][@"peopleCount"]intValue];
            int ass = (100/as)*aa;
            cell.redView.frame =CGRectMake(0,0, ass, 6);
            
            if ([_bigMUArr[indexPath.row][@"rewardType"]intValue]==1001){
                cell.jindouLbael.text = [NSString stringWithFormat:@"%@ 元宝",_bigMUArr[indexPath.row][@"everyoneAmount"]];//金豆
            }
            if ([_bigMUArr[indexPath.row][@"rewardType"]intValue]==1002){
                cell.jindouLbael.text = [NSString stringWithFormat:@"%@ 金币",_bigMUArr[indexPath.row][@"everyoneAmount"]];//金豆
            }
            if ([_bigMUArr[indexPath.row][@"rewardType"]intValue]==1003){
                cell.jindouLbael.text = [NSString stringWithFormat:@"%@ 金豆",_bigMUArr[indexPath.row][@"everyoneAmount"]];//金豆
            }
            if ([_bigMUArr[indexPath.row][@"rewardType"]intValue]==1004){
                cell.jindouLbael.text = [NSString stringWithFormat:@"%@ 额度",_bigMUArr[indexPath.row][@"everyoneAmount"]];//金豆
            }
            if ([_bigMUArr[indexPath.row][@"rewardType"]intValue]==1005){
                cell.jindouLbael.text = [NSString stringWithFormat:@"%@ 成长值",_bigMUArr[indexPath.row][@"everyoneAmount"]];//金豆
            }
            
            
            

            cell.canyuLbale.text = [NSString stringWithFormat:@"参与人数 %@/50",_bigMUArr[indexPath.row][@"counts"]];//参与
        }
    }else{
        //参与任务
        cell.canyuLbale.text = [NSString stringWithFormat:@"参与人数 %@/%@",_cainiarray[indexPath.row][@"finCounts"],_cainiarray[indexPath.row][@"peopleCount"]];
        int aa = [_cainiarray[indexPath.row][@"finCounts"]intValue];
        int as = [_cainiarray[indexPath.row][@"peopleCount"]intValue];
        int ass = (100/as)*aa;
        cell.redView.frame =CGRectMake(0,0, ass, 6);
        
        if (_cainiarray.count>0) {
            cell.titleLabel.text = [NSString stringWithFormat:@"%@",_cainiarray[indexPath.row][@"title"]];//b标题
            
            cell.jindouLbael.text = [NSString stringWithFormat:@"%@ 金豆",_cainiarray[indexPath.row][@"everyoneAmount"]];//金豆
            cell.canyuLbale.text = [NSString stringWithFormat:@"参与人数 %@/50",_cainiarray[indexPath.row][@"counts"]];//参与
        }
    }
//    [cell.renwuBtn addTarget:self action:@selector(chankanrenwuBtnClcike) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    RenWuXiangQingViewController*vc=[RenWuXiangQingViewController new];
    if (indexPath.section==0) {
        vc.idString = _bigMUArr[indexPath.row][@"id"];
    }else{
        vc.idString = _cainiarray[indexPath.row][@"id"];
    }
    
    vc.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:vc animated:YES];
}
//全部任务
-(void)httpRenwuUrl{
    NSLog(@"%@",CIT_number);
    NSString *nub = [NSString stringWithFormat:@"%@",CIT_number];
    NSString *url = [NSString stringWithFormat:@"%@%@",LINK_BASE_URL(@"task/taskAllList.api?access_token="),NSUD_Obj(@"token")];
    [HTTPURL postRequest:url parameters:@{@"pageIndex":@(page),@"areaId":nub} success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([responseObject[@"retFlag"]integerValue]==1) {
        NSLog(@"%@",responseObject);
        NSDictionary *dic = [PublicTools dictionaryWithJsonString:responseObject[@"data"]];
        _bigMUArr = [NSMutableArray array];
        _bigMUArr = dic[@"rows"];
        [tabView reloadData];
        
        }
    } filure:^(NSURLSessionDataTask *task, id error) {
        NSLog(@"%@",error);
    }];
}
//猜你喜欢
-(void)httpurlRquestcainixihuan{
    NSLog(@"%@",CIT_number);
   
    NSString *nub = [NSString stringWithFormat:@"%@",CIT_number];
    NSString *url = [NSString stringWithFormat:@"%@%@",LINK_BASE_URL(@"task/taskAllList.api?access_token="),NSUD_Obj(@"token")];
    [HTTPURL postRequest:url parameters:@{@"userId":userid,@"areaId":nub} success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@",responseObject);
        if ([responseObject[@"retFlag"]integerValue]==1) {
        NSDictionary *dic = [PublicTools dictionaryWithJsonString:responseObject[@"data"]];
        _cainiarray = [NSArray array];
        _cainiarray = dic[@"rows"];
        NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:1];
        [tabView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
        }
    } filure:^(NSURLSessionDataTask *task, id error) {
        NSLog(@"%@",error);
    }];
}
//cell 分割线 两端封头 实现这个两个方法 1
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
